package nanyang.com.dig88.Util;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;

/**
 * Created by Administrator on 2016/8/19.
 */
public class VitamioHelper {
    private static final String TAG = "Vitamio";
    private SurfaceView mPreview;
    private TextView promptTv;
    private SurfaceHolder holder;
    private Context context;
    private MediaPlayer mMediaPlayer;
    private boolean mIsVideoSizeKnown;
    private boolean mIsVideoReadyToBePlayed;
    private int mVideoWidth;
    private int mVideoHeight;
    private String videoUrl;
    private boolean isPlaying=false;
    static VitamioHelper instance=null;

    public static VitamioHelper getInstance(Context context, SurfaceView mPreview, TextView promptTv){
        if(instance==null){
            instance=new VitamioHelper();
        }
        instance.context=context;
        instance.mPreview=mPreview;
        instance.promptTv=promptTv;
        return instance;
    }

    public void init(String videoUrl){
        if (!LibsChecker.checkVitamioLibs((Activity) context))
            return;
        this.videoUrl =videoUrl;
        holder = mPreview.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Log.d(TAG,"surfaceCreated");
                playVideo();
                promptTv.setVisibility(View.VISIBLE);
                promptTv.setText("loading...");
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                Log.d(TAG,"surfaceChanged");
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                Log.d(TAG,"surfaceDestroyed");
            }
        });
        holder.setFormat(PixelFormat.RGBA_8888);
    }
    public void playVideo() {
        doCleanUp();
        try {
            //"rtmp://103.1.227.28:1935/live2/macau_lottery_B36";

            mMediaPlayer = new MediaPlayer(context);
            Log.d(TAG,videoUrl+"------>rtmp://103.1.227.28:1935/live2/macau_lottery_B36%2Blive=1");
            Map<String,String> dataMap=new HashMap();
            dataMap.put("max_analyze_duration","1000");
            dataMap.put("probesize","2048");
            mMediaPlayer.setDataSource(videoUrl,dataMap);
            mMediaPlayer.setDisplay(holder);
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                @Override
                public void onBufferingUpdate(MediaPlayer mp, int percent) {
                    Log.d(TAG,"onBufferingUpdate");
                }
            });
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    Log.d(TAG, "onPrepared called");
                    mIsVideoReadyToBePlayed = true;
                    startVideoPlayback();

                }
            });

            mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                    Log.d(TAG,"onVideoSizeChanged");
                    mIsVideoSizeKnown = true;
                    mVideoWidth = width;
                    mVideoHeight = height;
                    startVideoPlayback();
                }
            });
            mMediaPlayer.setVideoQuality(MediaPlayer.VIDEOQUALITY_LOW);
            mMediaPlayer.setBufferSize(512);

        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }
    }
    public void releaseMediaPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
            isPlaying=false;
            Log.e(TAG, "mMediaPlayer:releaseMediaPlayer ");
        }
    }
    public  void pauseMediaPlayer(){
        if (mMediaPlayer != null) {
            mMediaPlayer.pause();
            promptTv.setVisibility(View.VISIBLE);
            promptTv.setText("Video Pause");
            isPlaying=false;
        }
    }

    private void doCleanUp() {
        mVideoWidth = 0;
        mVideoHeight = 0;
        mIsVideoReadyToBePlayed = false;
        mIsVideoSizeKnown = false;
    }

    public void startVideoPlayback() {

        if(!isPlaying&&mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            Log.d(TAG, "startVideoPlayback");
            holder.setFixedSize(mVideoWidth, mVideoHeight);
            mMediaPlayer.start();
            isPlaying = true;
            promptTv.setVisibility(View.GONE);
        }

    }
}
